package com.socket.listener;

import com.socket.websocket.netty.WebSocketServer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * Created by kaddddd on 2017/7/13.
 */
@Component
public class SpringAfterInitListener implements ApplicationListener<ContextRefreshedEvent> {
    //spring容器加载完毕做一件事情（利用ContextRefreshedEvent事件）
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRereshedEvent) {
        if (contextRereshedEvent.getApplicationContext().getParent() == null){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("开启子线程去启动websocket!");
                    try {
                        WebSocketServer.build(8091).run();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
